/** 
Ok lets go through this then.

1) I create AI(some starting angle,power and the wind). Happy days, all good so far.

2) I run adjust every turn, and if its the AIs turn it automagically runs. Good

3) It then (or now) uses the power and angle I put in at the start, calculates how far it goes.

4) Depending on how far it goes it does some magic and changes the angles inside the class

5) I can get those angle, powers with functions and fire

*/

/**
 * 
 * @creator peak
 *
 */


public class AI2 {
	
	
	private double angle, power, wind;

	
	private ProjectileObject ball;
	
	private boolean isRight;
	
	// Takes input angle and power, the wind on the day, the ball its moving and the side its on
	public AI2 (double anglei, double poweri, double windi, ProjectileObject balli,boolean player) {
		this.angle = anglei;
		this.power = poweri;
		this.wind = windi;
		this.ball = balli;
		this.isRight = player;
	}
	
	public double getAngle() {
		return this.angle;
	}
	
	public double getPower() {
		return this.power;
	}
	
	// Function to allow adjusting of the ball - Peak wrote this
	public void adjust() {					//s = ut + 1/2at^2... Used to find position of projectile when it lands.
		double g = 0.05;													//Gravity!
		double ux = power * Math.cos(angle);								//Splitting Power into an x vector.
		double uy = power * Math.sin(angle);								//Same thing but into a y vector
		double sx;
		if(isRight) {
			sx = ux * ((2*uy) / g);
		}
		else sx = ux * ((-2*uy) / g); 									//Calculating distance from tank where the projectile landed.
		double tankx = 0,tanky = 0,etankx = 0,etanky = 0;
		if(isRight) {
			tankx = 20;
			tanky = 248;
			etankx = 830;
			etanky = 248;
					
		}
		else {
			tankx = 830;
			tanky = 248;
			etankx = 20;
			etanky = 248;	
		}
		double dist = sx - tankx; 									//Uhhhh, the distance the projectile landed from the target.
		double minangle = Math.acos(8.1);
		double maxangle = 60;
		
		if (ball.getWho() == 1) {
                        
			if (Math.abs(dist) < 100) {
                                
				if (dist > 0) {													//If the projectile went overhead...
                                         
					ux -= wind + Math.random() * (dist / 100.00);					//Assuming that the AI shoots within 100 pixels of the enemy tank...
					this.power = (ux / (Math.sin(angle)));
					}
				
				else if (dist < 0) {											//If the projectile didn't reach the target...
					ux += wind + Math.random() * (Math.abs(dist) / 100.00);		//Again assuming the same as above...
					this.power = (ux / (Math.sin(angle)));
					}
			}
			
			else if (Math.abs(dist) > 100) {
				if (dist > 0) {													//If the projectile went overhead...
					ux -= wind + Math.random() * (dist / 75.00);					//Assuming that the AI shoots within 100 pixels of the enemy tank...
					this.power = (ux / (Math.sin(angle)));
					}
				else if (dist < 0) {											//If the projectile didn't reach the target...
					ux += wind + Math.random() * (Math.abs(dist) / 75.00);		//Again assuming the same as above...
					this.power = (ux / (Math.sin(angle)));
					}
			}
			else System.out.println("There's a problem here somewhere. (Projectile Calculations)");
		}
	}
}